home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Talking KeyBoard / Source / AppleEvents / aesetdata.c < prev    next >
Encoding:
Text File  |  1998-06-06  |  4.8 KB  |  175 lines  |  [TEXT/CWIE]

  1. // Program Author: Paul Baxter
  2. //    pbaxter@assistivetech.com
  3. //
  4. //
  5. #include <Speech.h>
  6. #include <Ctype.h>
  7. #include <DeskBus.h>
  8. #include <Retrace.h>
  9.  
  10. #include "pref.h"
  11. #include "globals.h"
  12. #include "menu.h"
  13. #include "aeutils.h"
  14. #include "aesetdata.h"
  15. #include "aetoken.h"
  16. #include "command.h"
  17.  
  18. // * ****************************************************************************** *
  19. // *    DoSetData
  20. // *                Handles the SetData Apple Event, extracting the direct
  21. // *                object (which says what to set) and the data (what to set
  22. //    *                it to).
  23. // * ****************************************************************************** *
  24. pascal OSErr DoSetData(const AppleEvent    *theAppleEvent, AppleEvent    *reply, long handlerRefCon)
  25. {
  26. #pragma unused (reply, handlerRefCon)
  27.     AEDesc     directObj = {typeNull, NULL},
  28.             dataDesc = {typeNull, NULL};
  29.     OSErr      err;
  30.             
  31.         // pick up the direct object, which is the object whose data is to be set
  32.     err = AEGetParamDesc(theAppleEvent,  keyDirectObject, typeWildCard, &directObj);
  33.     if (noErr != err) goto done;
  34.  
  35.         // now the data to set it to - typeWildCard means get as is
  36.         // e.g. this is the name of the font for text
  37.     err = AEGetParamDesc(theAppleEvent, keyAEData, typeWildCard, &dataDesc);
  38.     if (noErr != err) goto done;
  39.     
  40.         // missing any parameters?
  41.     err = GotRequiredParams(theAppleEvent);
  42.     if (noErr != err) goto done;
  43.     
  44.         // set the data
  45.     err = HandleSetData(&directObj, &dataDesc);
  46.  
  47. done:            
  48.     if (directObj.dataHandle)
  49.         AEDisposeDesc(&directObj);
  50.     if (dataDesc.dataHandle)
  51.         AEDisposeDesc(&dataDesc);
  52.  
  53.     return(err);
  54. }    // DoSetData
  55.  
  56.  
  57. // * ****************************************************************************** *
  58. // *    HandleSetData
  59. // *                        Resolves the object into a token (could be one of 
  60. // *                        many) andthe sets the data of that object to dataDesc.
  61. // * ****************************************************************************** *
  62. OSErr HandleSetData(const AEDesc *theObj, AEDesc *dataDesc)
  63. {
  64.     AEDesc          objTokenDesc = {typeNull, NULL},
  65.                     itemDesc = {typeNull, NULL},
  66.                     ignoreResult = {typeNull, NULL};
  67.     long            index;
  68.     DescType        returnedType;
  69.     OSErr           err;
  70.  
  71.  
  72.     //    Coerce theObj into a token which we can use - 
  73.     //         set the property or data for that token
  74.     if (typeObjectSpecifier == theObj->descriptorType)
  75.         err = AEResolve(theObj, kAEIDoMinimum, &objTokenDesc);
  76.     else    // Otherwise, just copy it
  77.         err = AEDuplicateDesc(theObj, &objTokenDesc);
  78.  
  79.     if (noErr != err) goto done;
  80.             
  81.     switch (objTokenDesc.descriptorType) {
  82.  
  83.         case typeMyApplProp:
  84.             err = SetAppProperty(&objTokenDesc, dataDesc);
  85.             break;
  86.  
  87.         case typeAEList:                // If it's a list then do each item
  88.             err = AECountItems(&objTokenDesc, &index);
  89.             if (noErr != err) goto done;
  90.  
  91.             for (; index > 0; index--) {
  92.                 err = AEGetNthDesc(&objTokenDesc, index, typeWildCard, &returnedType, &itemDesc);
  93.  
  94.                 if (noErr == err)        // Get property by calling this function again
  95.                     err = HandleSetData(&itemDesc, dataDesc);
  96.                 
  97.                 if (itemDesc.dataHandle)
  98.                     AEDisposeDesc(&itemDesc);
  99.             }
  100.             break;
  101.             
  102.         default:
  103.             err = errAEWrongDataType;
  104.     }
  105.  
  106. done:
  107.     if (objTokenDesc.dataHandle)    
  108.         AEDisposeDesc(&objTokenDesc);
  109.     if (itemDesc.dataHandle)    
  110.         AEDisposeDesc(&itemDesc);
  111.     if (ignoreResult.dataHandle)
  112.         AEDisposeDesc(&ignoreResult);
  113.  
  114.     return(err);
  115. } // HandleSetData
  116.  
  117.  
  118. // * ****************************************************************************** *
  119. // *    SetAppProperty
  120. // *            Sets the property specified in theAPPPropToken to
  121. // *            be that supplied in dataDesc.
  122. // * ****************************************************************************** *
  123. OSErr SetAppProperty(const AEDesc *theWPTokenDesc, const AEDesc *dataDesc)
  124. {
  125.     ApplPropToken    theAPPPropToken;
  126.     AEDesc          aDesc = {typeNull, NULL},
  127.                     ignoreResult = {typeNull, NULL};
  128.     Size            tokenSize;
  129.     long            value;
  130.     Str255            newvoice;
  131.     OSErr           err;
  132.  
  133.     err = AECoerceDesc(theWPTokenDesc, typeMyApplProp, &aDesc);
  134.     if (!err) {
  135.  
  136.         GetRawDataFromDescriptor(&aDesc, (Ptr)&theAPPPropToken,
  137.                                         sizeof(theAPPPropToken), &tokenSize);
  138.  
  139.         switch (theAPPPropToken.tokenApplProperty) {
  140.             case typeVoiceProperty:
  141.                 err = GetPStringFromDescriptor(dataDesc, newvoice);
  142.                 if (!err) {
  143.                     ProcessCommand(CHANGEVALUE(VoiceCmd, kStringValue), newvoice);
  144.                 }
  145.                 break;
  146.  
  147.             case typeSpeakCharProperty:
  148.                 err = GetLongIntFromDescriptor(dataDesc,  &value);
  149.                 if (!err) {
  150.                     ProcessCommand(CHANGEVALUE(SpeakCharsCmd, value), nil);
  151.                 }
  152.                 break;
  153.  
  154.             case typeSpeakWordProperty:
  155.                 err = GetLongIntFromDescriptor(dataDesc,  &value);
  156.                 if (!err) {
  157.                     ProcessCommand(CHANGEVALUE(SpeakWordsCmd, value), nil);
  158.                 }
  159.                 break;
  160.  
  161.             case typeSpeakSentenceProperty:
  162.                 err = GetLongIntFromDescriptor(dataDesc, &value);
  163.                 if (!err) {
  164.                     ProcessCommand(CHANGEVALUE(SpeakSentencesCmd, value), nil);
  165.                 }
  166.                 break;
  167.         }
  168.     }
  169.     if (aDesc.dataHandle)    
  170.         AEDisposeDesc(&aDesc);
  171.     if (ignoreResult.dataHandle)    
  172.         AEDisposeDesc(&ignoreResult);
  173.     return err;
  174. }
  175.